

#include "puzzleSettings.h"

// CURRENTLY IMPLEMENTED AS ARRAY OF INTEGERS.
class puzzle{

public:
	pos board[size];
	pos emptyPos;

	puzzle(){
		//board is initialized by outside code.
	}

	~puzzle(){
		//nothing to delete since no memory allocated.
	}



	 /*
	  * Checks direction to make sure it is a valid move from this board configuration.
	  */
	 /*bool validMove(int direction){
		 pos rowEmpty = emptyPos /width;
		 pos colEmpty = emptyPos %width;
		 switch(direction){
		 	case NORTH: { // move NORTH, fails if on edge (row = 0);
		 		if(rowEmpty == 0 )
		 			return false; //can't move up.
		 		return true;
		 	}
		 	case SOUTH: { // move SOUTH, fails if on edge (row = size-1);
		 		if(rowEmpty == width-1)
		 			return false; //can't move down.
		 		return true;
		 	}
		 	case EAST: { // move EAST, fails if on edge (col = size-1);
		 		if(colEmpty == width-1)
		 			return false; //can't move right.
		 		return true;
		 	}
		 	case WEST: { // move WEST, fails if on edge (col = 0);
		 		if(colEmpty == 0)
		 			return false; //can't move left.
		 		return true;
		 	}
		 	default: return false; // not a valid direction
		 }
	 }*/

	/*
	 * Changes the child board to be the specified move and returns true if move was doable, false otherwise.
	 * If move is not doable then child board is not changed.
	 *
	 * NOTE: one possible optimization is to memcopy this board onto that board.
	 */
	bool load(puzzle & active, int direction){


		for(int i = 0; i < size; i++) //can make move, first copy board
			board[i] = active.board[i] ;

		switch(direction){ // set empty location in new board.
			case NORTH: { // move NORTH (subtract 1 from row)
				emptyPos = active.emptyPos-width; //mark new empty position
				//if(debug1) cout << "NORTH" << endl;
				break;
			}
			case SOUTH: { // move SOUTH, add 1 to row
				emptyPos = active.emptyPos+width; //mark new empty position
				//if(debug1) cout << "SOUTH" << endl;
				break;
			}
			case EAST: { // move EAST, add 1 to column
				emptyPos = active.emptyPos+1; //mark new empty position
				//if(debug1) cout << "EAST" << endl;
				break;
			}
			case WEST: { // move WEST, subtract 1 from column
				emptyPos = active.emptyPos-1; //mark new empty position
				//if(debug1) cout << "WEST" << endl;
				break;
			}
		}

		//SWAP EMPTY POSITION IN NEW BOARD.
		board[active.emptyPos] = board[emptyPos];

		board[emptyPos] = 0;

		return true;
	}

	/*
	 * Function not used in algorithm.  Currently set for numbers up to 4 decimal digits wide.
	 */
	void printBoard(){

		for(int i = 0; i < size; i++){
			if(i % width == 0)
				cout << endl;  //newline for every width of board characters.
			pos tile = board[i];
			if(tile >= 1000)
				cout << " "; //no extra padding, just the normal separating space.
			else if (tile >= 100)
				cout << "  "; //need one extra padding for thousands digit.
			else if(tile >= 10)
				cout << "   "; //need two extra padding for hundred and thousand digit
			else
				cout << "    "; //need 3 padding spots for the single digit.

			if(tile==0){
				cout << "b";
			}
			else{
				cout << (int)tile; //output the tile value;
			}
		}
		cout << endl;
		cout << "--------------------------------------------------" <<endl;

	}

};
